import multiprocessing
import threading
import time


class Singleton():
    instance = None
    lock = threading.Lock()

    def __init__(self, name):
        self.name = name

    def __new__(cls, *args, **kwargs):
        if cls.instance:
            return cls.instance

        with cls.lock:
            if cls.instance:
                return cls.instance
            time.sleep(0.1)
            cls.instance = object.__new__(cls)
            return cls.instance


def task():
    obj = Singleton('obj')
    print(f"{obj}\n",end="")


if __name__ == '__main__':
    for i in range(10):
        t = threading.Thread(target=task)
        t.start()
    print("cpu_count:",multiprocessing.cpu_count())
